package com.health.tools;

import java.text.SimpleDateFormat;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.viewscontrols.NovaApplication;
import com.health.db.FDbManager;
import com.health.model.UpDataReturn;
import com.health.tablebean.JTYSDoctorContInfoBean;

import android.text.TextUtils;
import android.util.Log;

/**
 * 解析服务项目包
 * 
 * @author nova
 *
 */
public class JX_FWXMB {
	private final static String TAG = "JX_FWXMB";
	static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static FDbManager fdbManager = NovaApplication.fdbmanger;

	/**
	 * 解析自查填报表数据
	 * 
	 * @author 王婷婷
	 * @date 2017年4月13日 上午9:31:11
	 */
	public static UpDataReturn jxFwxmbData(String result) {
		UpDataReturn updata = null;
		int successCount = 0;
		try {
			JSONObject root = new JSONObject(result.toString());
			if (root.has("success")) {
				if (root.getBoolean("success")) {
					if (root.has("infor")) {
						JSONArray jsons = root.getJSONArray("infor");
						int length = jsons.length();
						Log.w("自查填报表----size", "" + length);
						// System.out.print(result);
						if (length > 0) {
							
							List<JTYSDoctorContInfoBean> beans= NovaApplication.fdbmanger.findAllByWhere(JTYSDoctorContInfoBean.class, "officeId='"+NovaApplication.getJgbh()+"'");
							if(beans.size()>0){
								NovaApplication.fdbmanger.deleteByWhere(JTYSDoctorContInfoBean.class, "officeId='"+NovaApplication.getJgbh()+"'");
							}
							
							updata = new UpDataReturn();
							updata.setSize(length);
							for (int i = 0; i < length; i++) {
								JSONObject obj = jsons.getJSONObject(i);

								JTYSDoctorContInfoBean fwxm = new JTYSDoctorContInfoBean();
////					            "apptype": "1",
//					            "contractServiceItemContent": "慢性病患者在享受中心高血压、糖尿病俱乐部互动同时，免费参加五禽戏、太极拳、太极剑等传统保健运动培训。",
//					            "contractServiceItemKs": "健康教育",
//					            "contractServiceItemType": "12传统保健强体",
//					            "contractServiceType": "1",
//					            "createdate": "2017-03-15 14:26:54",
//					            "fee_scale_desc": "暂定120次/元",
//					            "guid_price": "120",
//					            "id": "cae41f32cf1b48d28e83dcb9e9f9a9f5",
//					            "itemType": "120",
//					            "officeId": "1",
//					            "officeName": "山东公司",
//					            "pageCount": 0,
//					            "pageNo": 0,
//					            "pageSize": 0,
//					            "person_type": "11",
//					            "person_type_name": "暂未分类"
								fwxm.setId(obj.getString("id"));
								fwxm.setContractServiceItemContent(obj.getString("contractServiceItemContent"));
								fwxm.setContractServiceItemKs(obj.getString("contractServiceItemKs"));
								fwxm.setContractServiceItemType(obj.getString("contractServiceItemType"));
								fwxm.setContractServiceType(obj.getString("contractServiceType"));
								if(obj.has("createdate")){
								fwxm.setCreatedate(obj.getString("createdate"));
								}else{
									fwxm.setCreatedate("");
								}
								fwxm.setItemType(obj.getString("itemType"));
								fwxm.setOfficeId(NovaApplication.getJgbh());
								fwxm.setOfficeName(obj.optString("officeName"));
								if (obj.has("fee_scale_desc")) {
									fwxm.setFee_scale_desc(obj.getString("fee_scale_desc"));
								} else {
									fwxm.setFee_scale_desc("");
								}
								if (obj.has("guid_price")) {
									fwxm.setGuid_price(obj.getString("guid_price"));
								} else {
									fwxm.setGuid_price("");
								}
								if (obj.has("person_type")) {
									fwxm.setPerson_type(obj.getString("person_type"));
									if (!TextUtils.isEmpty(obj.getString("person_type"))){
										if ("DM102-11_1".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("老年人");
										}else if ("DM102-11_2".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("孕产妇");
										}else if ("DM102-11_3".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name(" 儿童");
										}else if ("DM102-11_4".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("残疾人");
										}else if ("DM102-11_5".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("高血压患者");
										}else if ("DM102-11_6".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("糖尿病患者");
										}else if ("DM102-11_7".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("严重精神障碍患者");
										}else if ("DM102-11_8".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("慢阻肺");
										}else if ("DM102-11_9".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("脑卒中患者");
										}else if ("DM102-11_10".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("冠心病患者");
										}else if ("DM102-11_11".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("暂未分类");
										}else if ("DM102-11_12".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("一般人群");
										}else if ("DM102-11_13".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("计划生育特殊家庭");
										}else if ("DM102-11_14".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("卡外贫困户");
										}else if ("DM102-11_15".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("低保户");
										}else if ("DM102-11_16".equals(obj.getString("person_type"))){
											fwxm.setPerson_type_name("建档立卡贫困户");
										}
									}else {
										fwxm.setPerson_type_name("");
									}
								} else {
									fwxm.setPerson_type("");
								}
//								if (obj.has("person_type_name")) {
//									fwxm.setPerson_type_name(obj.getString("person_type_name"));
//								}else{
//									fwxm.setPerson_type_name("");
//								}

								
								if (obj.has("officeType")) {
									fwxm.setOfficeType(obj.getString("officeType"));
								} else {
									fwxm.setOfficeType("");
								}
								
								Log.w(TAG, "保存数据fwxm\n"+fwxm.toString());
								try {
									NovaApplication.fdbmanger.save(fwxm);
								} catch (Exception e) {
									e.printStackTrace();
								}
								
								successCount = successCount + 1;
							}
							updata.setSuccessCount(successCount);
							updata.setFailCount(length - successCount);
						}
						

					}
				}
			}
			

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return updata;

	}

}
